<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<title>网上购票</title>
<link rel="stylesheet" href="resources/css/frozen.css">
<link rel="stylesheet" href="resources/css/main.css">
<script src="resources/lib/zepto.min.js"></script>
<script src="resources/js/frozen.js"></script>
<script src="resources/js/config.js"></script>
<script src="resources/js/main.js"></script>

</head>
<body ontouchstart="">
	<header class="ui-header ui-header-positive ui-border-b">
		<i class="ui-icon-return" onclick="history.back()"></i>
		<h1>编辑联系人</h1>
	</header>
	<footer class="ui-footer ui-footer-btn">
		<ul id="footer" class="ui-tiled ui-border-t">
			<li data-href="index.html" class="ui-border-r">
				<div>查询购票</div>
			</li>
			<li data-href="order.html" class="ui-border-r">
				<div>订单查询</div>
			</li>
			<li data-href="member.html">
				<div>会员中心</div>
			</li>
		</ul>
	</footer>
	<section class="ui-container">
		<section class="ui-selector">
			<br/>
			<div class="ui-form ui-border-t">
				<form action="#">
					<input id="friend-id" type="hidden" value="">
					<div class="ui-form-item ui-form-item-show ui-border-b">
						<label for="friend-name">姓名</label>
						<input id="friend-name" type="text" value="">
					</div>
					<div class="ui-form-item ui-form-item-show ui-border-b">
						<label for="id-card">身份证号</label>
						<input id="id-card" type="text" value="">
					</div>
					<div class="ui-form-item ui-form-item-show ui-border-b">
						<label for="mobile">手机号</label>
						<input id="mobile" type="text" value="">
					</div>
				</form>
			</div>
			<div class="ui-btn-wrap">
				<button id="btn-update" class="btn-group ui-btn-lg ui-btn-primary">
					保存修改
				</button>
				<button id="btn-delete" class="btn-group ui-btn-lg ui-btn-danger">
					删&nbsp;&nbsp;&nbsp;&nbsp;除
				</button>
			</div>
		</section>
		<div class="ui-whitespace">
			<h1 class="ui-txt-warning">温馨提示：</h1>
			<p class="ui-txt-justify">互联网售票实行实名制，请准确填写乘车人基本信息。</p>
		</div>
	</section>

<script>
// 检查登录状态
checkLogin();

(function (){
	/* -------------------- 根据localStorage中的值，初始化界面 -------------------- */
	
	// 加载id
	$("#friend-id").val(sessionStorage.friendDetailId);
	
	// 加载姓名
	$("#friend-name").val(sessionStorage.friendDetailName);
	
	// 加载身份证号
	$("#id-card").val(sessionStorage.friendDetailIdCard);
	
	// 加载手机
	$("#mobile").val(sessionStorage.friendDetailMobile);
	
	// 保存修改按钮点击事件
	$("#btn-update").tap(function(){
		updateFriend();
	});
	
	// 删除按钮点击事件
	$("#btn-delete").tap(function(){
		deleteFriend();
	});
	
	// 底部主菜单初始化
	initFooter("#footer");
})();

function updateFriend() {
	showLoading("#loading");
	var friendId = $("#friend-id").val();
	var friendName = $.trim($("#friend-name").val());
	var idCard = $.trim($("#id-card").val());
	idCard = idCard.toUpperCase(); // 转大写
	$("#id-card").val(idCard);
	var mobile = $.trim($("#mobile").val());
	var action = "edit";
	
	// 身份证有效性校验
	if(!validateIdCard(idCard)) {
		showInfoTip("身份证号验证失败，请检查");
		return;
	}
	
	ajax({
		url: appConfig.baseUrl + "/BusTicketOrder.go",
		data: {
			method: "SaveMemberFriend",
			friendid: friendId,
			membername: friendName,
			cardid: idCard,
			mobile: mobile,
			action: action
		},
		success: function(data) {
			hideLoading("#loading");
			
			if(!!data.success) {
				sessionStorage.friendDetailName = friendName;
				sessionStorage.friendDetailIdCard = idCard;
				sessionStorage.friendDetailMobile = mobile;
		
				sessionStorage.message = "修改成功";
				history.back();
			} else {
				showInfoTip("修改失败");
			}
		}
	});
}

function deleteFriend() {
	showLoading("#loading");
	var friendId = $("#friend-id").val();
	var friendName = $.trim($("#friend-name").val());
	var idCard = $.trim($("#id-card").val());
	idCard = idCard.toUpperCase(); // 转大写
	$("#id-card").val(idCard);
	var mobile = $.trim($("#mobile").val());
	var action = "edit";
	
	// 身份证有效性校验
	if(!validateIdCard(idCard)) {
		showInfoTip("身份证号验证失败，请检查");
		return;
	}
	
	ajax({
		url: appConfig.baseUrl + "/BusTicketOrder.go",
		data: {
			method: "DeleteMemberFriend",
			friendid: friendId
		},
		failMessage: "删除失败，请重试",
		success: function(data) {
			hideLoading("#loading");
			
			if(!!data.success) {
				sessionStorage.message = "删除成功";
				history.back();
			} else {
				showInfoTip("删除失败");
			}
		}
	});
}
</script>
</body>
</html>